package com.symaster.common.swing.ui.scroll;

import com.symaster.common.swing.style.Style;

import javax.swing.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
import java.awt.*;

/**
 * @author yinmiao
 * @date 2021-07-28 16:32
 */
public class MyJScrollPaneUI extends BasicScrollBarUI {

    public Color color_background = new Color(74, 71, 75);
    public Color color_thumbColor = new Color(194, 107, 92);

    @Override
    protected void configureScrollBarColors() {
        LookAndFeel.installColors(scrollbar, "ScrollBar.background",
                "ScrollBar.foreground");

        super.thumbColor = this.color_thumbColor;
        super.trackColor = this.color_background;
        super.trackHighlightColor = this.color_background;
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        c.setBackground(color_background);
        Rectangle thumbBounds = getThumbBounds();
        if (thumbBounds.intersects(g.getClipBounds())) {
            paintThumb(g, c, thumbBounds);
        }
    }

    @Override
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
        if (thumbBounds.isEmpty() || !scrollbar.isEnabled()) {
            return;
        }

        int w = thumbBounds.width - 4;
        int h = thumbBounds.height - 4;
        int arcw = 7;
        int arch = 7;
        int x = 2;
        int y = 2;

        g.translate(thumbBounds.x, thumbBounds.y);

        g.setColor(thumbColor);
        Graphics2D g2 = (Graphics2D) g;
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.addRenderingHints(rh);
        g2.fillRoundRect(x, y, w, h, arcw, arch);

        g.translate(-thumbBounds.x, -thumbBounds.y);
    }

    @Override
    protected JButton createDecreaseButton(int orientation) {
        // JButton decreaseButton = super.createDecreaseButton(orientation);
        // decreaseButton.setVisible(false);
        JButton jButton = new JButton();
        jButton.setBackground(new Color(40, 40, 40));
        Style.setBorder(1, jButton);
        return jButton;
    }

    @Override
    protected JButton createIncreaseButton(int orientation) {
        // JButton increaseButton = super.createIncreaseButton(orientation);
        // increaseButton.setBackground(new Color(40, 40, 40));
        // increaseButton.setVisible(false);

        JButton jButton = new JButton();
        jButton.setBackground(new Color(40, 40, 40));

        Style.setBorder(1, jButton);
        // jButton.setEnabled(false);
        return jButton;
    }

    @Override
    public Dimension getPreferredSize(JComponent c) {
        return new Dimension(10, 10);
    }
}
